"""
block_io.py
非阻塞IO演示
"""
from socket import *
from time import *


log = open("my.log",'a',buffering=1)

#监听套接字
sock = socket()
sock.bind(("0.0.0.0",8888))
sock.listen(5)

#设置套接字为非阻塞IO -》 sock调用所有函数都不阻塞
# sock.setblocking(False)

#设置超时时间
sock.settimeout(2)


while True:
    try:
        conn,addr = sock.accept()
        print("connect from",addr)
    except BlockingIOError as e:
        #做与accept无关的事
        sleep(2)
        msg = "%s : %s\n"%(ctime(),e)
        log.write(msg)
    except timeout as e:
        msg = "%s : %s\n" % (ctime(), e)
        log.write(msg)
    except KeyboardInterrupt:
        break
    else:
        data = conn.recv(1024)
        conn.send(b"Bye")
        print(data.decode())